//
//  JDDChousePikerView.m
//  JDD
//
//  Created by 张露杰 on 17/11/7.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "RWBChousePikerView.h"

@interface RWBChousePikerView()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) UIPickerView *piker;

@end

@implementation RWBChousePikerView
{
    NSArray *_titles;
}
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, 250*screenW_6);
        self.backgroundColor = [UIColor whiteColor];
        _titles = titles;
        [self creatView];
    }
    return self;
}

-(void)creatView
{
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    shareBtn.frame = CGRectMake(self.frame.size.width - 50*screenW_6, 0, 50*screenW_6, 50*screenW_6);
    [shareBtn setTitle:@"确认" forState:UIControlStateNormal];
    [shareBtn setTitleColor:mainColor forState:UIControlStateNormal];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:14*screenW_6];
    [self addSubview:shareBtn];
    [shareBtn addTarget:self action:@selector(chouseTitle) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.piker];
}

-(UIPickerView *)piker
{
    if (!_piker) {
        _piker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 50*screenW_6, self.frame.size.width, 200*screenW_6)];
        _piker.delegate = self;
        _piker.dataSource = self;
    }
    return _piker;
}

#pragma mark - pickerViewDelegateMethod

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _titles.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    return _titles[row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    //设置分割线的颜色
//    for(UIView *singleLine in pickerView.subviews)
//    {
//        if (singleLine.frame.size.height < 1)
//        {
//            singleLine.backgroundColor = mainColor;
//        }
//    }
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc]init];
        pickerLabel.font = [UIFont systemFontOfSize:20*screenW_6];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textColor = [UIColor blackColor];
        pickerLabel.textAlignment = NSTextAlignmentCenter;
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

-(void)chouseTitle
{
    if (_chouseTitleblock) {
        _chouseTitleblock(_titles[[_piker selectedRowInComponent:0]]);
    }
}

@end
